{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Pandas 字符串替换\n",
        "\n",
        "本教程介绍Pandas中字符串替换的方法，包括replace和translate。\n",
        "\n",
        "## 学习目标\n",
        "\n",
        "- 掌握str.replace()方法：替换子串（支持正则表达式）\n",
        "- 掌握str.translate()方法：字符映射替换\n",
        "- 了解替换的实际应用场景"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 导入库"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. 创建示例数据"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 简单替换\n",
        "df['电话_替换'] = df['电话'].str.replace('-', '')\n",
        "df['文本_替换'] = df['文本'].str.replace(' ', '_')\n",
        "\n",
        "print(\"简单替换示例：\")\n",
        "print(df[['电话', '电话_替换', '文本', '文本_替换']])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.1 使用正则表达式替换\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import re\n",
        "\n",
        "# 使用正则表达式替换\n",
        "df['邮箱_隐藏域名'] = df['邮箱'].str.replace(r'@.*', '@***', regex=True)\n",
        "df['电话_隐藏'] = df['电话'].str.replace(r'\\d{4}$', '****', regex=True)  # 隐藏后4位\n",
        "\n",
        "print(\"正则表达式替换示例：\")\n",
        "print(df[['邮箱', '邮箱_隐藏域名', '电话', '电话_隐藏']])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.2 限制替换次数\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 只替换第一个匹配\n",
        "texts = pd.Series(['hello hello hello', 'test test test'])\n",
        "df_n = pd.DataFrame({'文本': texts})\n",
        "df_n['替换1次'] = df_n['文本'].str.replace('hello', 'hi', n=1)\n",
        "df_n['替换全部'] = df_n['文本'].str.replace('hello', 'hi')\n",
        "\n",
        "print(\"限制替换次数：\")\n",
        "print(df_n)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.3 使用捕获组\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 使用捕获组重新排列\n",
        "dates = pd.Series(['2023-01-15', '2023-02-20', '2023-03-25'])\n",
        "df_dates = pd.DataFrame({'日期': dates})\n",
        "# 将 YYYY-MM-DD 格式转换为 DD/MM/YYYY\n",
        "df_dates['新格式'] = df_dates['日期'].str.replace(r'(\\d{4})-(\\d{2})-(\\d{2})', r'\\3/\\2/\\1', regex=True)\n",
        "\n",
        "print(\"使用捕获组：\")\n",
        "print(df_dates)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. str.translate() - 字符映射替换\n",
        "\n",
        "使用字符映射表进行字符级别的替换，适合批量替换多个字符。\n",
        "\n",
        "### 语法\n",
        "```python\n",
        "series.str.translate(table)\n",
        "```\n",
        "\n",
        "### 参数\n",
        "- `table`: 字符映射表（使用str.maketrans()创建）\n",
        "\n",
        "### 示例\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 创建字符映射表\n",
        "# 将 a->1, e->2, i->3, o->4, u->5\n",
        "translation_table = str.maketrans('aeiou', '12345')\n",
        "\n",
        "texts_translate = pd.Series(['hello', 'world', 'python', 'programming'])\n",
        "df_translate = pd.DataFrame({'文本': texts_translate})\n",
        "df_translate['替换后'] = df_translate['文本'].str.translate(translation_table)\n",
        "\n",
        "print(\"translate()方法示例：\")\n",
        "print(df_translate)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 3.1 删除字符\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 使用translate删除字符（将字符映射为None）\n",
        "delete_table = str.maketrans('', '', 'aeiou')  # 删除所有元音字母\n",
        "\n",
        "texts_delete = pd.Series(['hello', 'world', 'python'])\n",
        "df_delete = pd.DataFrame({'文本': texts_delete})\n",
        "df_delete['删除元音'] = df_delete['文本'].str.translate(delete_table)\n",
        "\n",
        "print(\"删除字符示例：\")\n",
        "print(df_delete)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4. 实际应用场景\n",
        "\n",
        "### 4.1 数据清洗：统一格式\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 统一电话号码格式\n",
        "phones = pd.DataFrame({\n",
        "    '电话': ['010-12345678', '021 87654321', '0755.11223344', '029_55667788']\n",
        "})\n",
        "\n",
        "# 统一替换所有分隔符为-\n",
        "phones['统一格式'] = phones['电话'].str.replace(r'[-\\s._]', '-', regex=True)\n",
        "\n",
        "print(\"统一格式示例：\")\n",
        "print(phones)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 4.2 数据脱敏：隐藏敏感信息\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 隐藏邮箱和手机号\n",
        "sensitive_data = pd.DataFrame({\n",
        "    '邮箱': ['zhang@example.com', 'li@test.com', 'wang@demo.com'],\n",
        "    '手机': ['13812345678', '13987654321', '15011223344']\n",
        "})\n",
        "\n",
        "# 隐藏邮箱用户名\n",
        "sensitive_data['邮箱_脱敏'] = sensitive_data['邮箱'].str.replace(r'^(.{2}).*(@)', r'\\1***\\2', regex=True)\n",
        "# 隐藏手机号中间4位\n",
        "sensitive_data['手机_脱敏'] = sensitive_data['手机'].str.replace(r'(\\d{3})\\d{4}(\\d{4})', r'\\1****\\2', regex=True)\n",
        "\n",
        "print(\"数据脱敏示例：\")\n",
        "print(sensitive_data)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 4.3 文本清理：移除特殊字符\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 移除所有标点符号\n",
        "import string\n",
        "\n",
        "texts_clean = pd.DataFrame({\n",
        "    '文本': ['Hello, World!', 'Python?', 'Data-Science.', 'Machine_Learning!']\n",
        "})\n",
        "\n",
        "# 移除所有标点符号\n",
        "texts_clean['清理后'] = texts_clean['文本'].str.replace(r'[^\\w\\s]', '', regex=True)\n",
        "\n",
        "print(\"文本清理示例：\")\n",
        "print(texts_clean)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5. 方法对比总结\n",
        "\n",
        "| 方法 | 功能 | 适用场景 | 性能 |\n",
        "|------|------|---------|------|\n",
        "| `str.replace()` | 替换子串或正则模式 | 复杂替换、正则表达式 | 中等 |\n",
        "| `str.translate()` | 字符映射替换 | 批量字符替换、删除字符 | 快速 |\n",
        "\n",
        "### 选择建议\n",
        "\n",
        "- **简单替换**: 使用 `replace()` - 直观易用\n",
        "- **正则替换**: 使用 `replace()` 配合正则表达式\n",
        "- **批量字符替换**: 使用 `translate()` - 性能更好\n",
        "- **删除字符**: 使用 `translate()` - 更高效\n",
        "\n",
        "## 6. 总结\n",
        "\n",
        "### 关键要点\n",
        "\n",
        "1. **replace()**: 最常用，支持正则表达式，功能强大\n",
        "2. **translate()**: 适合批量字符替换，性能更好\n",
        "3. **正则表达式**: replace()支持复杂的模式匹配和捕获组\n",
        "4. **应用场景**: 数据清洗、格式统一、数据脱敏\n",
        "\n",
        "### 使用建议\n",
        "\n",
        "- **数据清洗**: 使用replace()统一格式\n",
        "- **数据脱敏**: 使用replace()配合正则表达式隐藏敏感信息\n",
        "- **批量替换**: 使用translate()提高性能\n",
        "- **链式调用**: 可以与其他字符串方法链式调用\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.8.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
